#include "based_operation.h"

Status InitStack(Stack &s)
{
    s.element=(eleType *)malloc(sizeof(eleType)*INITSIZE);
    if(s.element==NULL)
    {
        printf("OVERFLOW!\n");
        return OVERFLOW;
    }
    s.capacity=INITSIZE;
    s.size=0;

    return OK;
}

Status DestroyStack(Stack &s)
{
    if(s.element!=NULL)
    {
        free(s.element);
        s.element=NULL;
    }
    s.capacity=0;
    s.size=0;
}

Status ClearStack(Stack &s)
{
    s.size=0;
    return OK;
}

bool IsEmpty(Stack &s)
{
    if(s.size==0)
    {
        return true;
    }
    return false;
}

Status Pop(Stack &s,eleType &element)
{
    if(IsEmpty(s))
    {
        return ERROR;
    }

    element=s.element[--s.size];
    return OK;
}

Status Push(Stack &s,const eleType &element)
{
    if(s.size>=s.capacity)
    {
        eleType *newbase=(eleType*)malloc(sizeof(eleType)*s.capacity*1.5);
        if(newbase==NULL)
        {
            printf("OVERFLOW!\n");
            return OVERFLOW;
        }
        s.element=newbase;
        s.capacity*=1.5;
    }
    s.element[s.size++]=element;
    return OK;
}

